<?php
/**
 * 地产服务类
 */

namespace app\index\service;

use Config;
use Cache;
use think\Db;
use app\index\model\MemberOrderVip;
use think\Request;
use app\common\library\Payment;

class Order
{   
    /**
     * 购买vip
     * @param unknown $member_id
     * @param unknown $type // 1 vip 388 2 超级VIP 998 3 金钻会员 3980
     */
    public function createOrder($memberInfo, $type, $pay_type)
    {
        if ($memberInfo->is_vip >= $type) {
            exception('我已经是VIP');
        }
        $price = [ 1 => 388, 2 => 988, 3 => 3980];
        $data['member_id'] = $memberInfo->member_id;
        $data['type'] = $type;
        $data['pay_type'] = $pay_type;
        $data['order_sn'] = $this->createNO('VIP');
        $data['total_amount'] = $price[$type];
        $data['title'] = '升级VIP'.$price[$type];
        $data['create_time'] = time();
        $MemberOrderVipModel = new MemberOrderVip();
        $res = $MemberOrderVipModel->insertGetId($data);
        if (!$res) {
            exception('创建订单失败');
        }
        $this->payment($data, $memberInfo, $MemberOrderVipModel, $res);
    }
    
    private function payment($orderInfo, $memberInfo, $MemberOrderVipModel, $orderid)
    {
        $paymemt = [];
        if ($orderInfo['pay_type'] == 1) {
            //微信支付
            $params['title'] = $orderInfo['title'];
            $params['tid'] = $orderInfo['order_sn'];
            $params['fee'] = $orderInfo['total_amount'] * 100;
            $params['ip'] = request()->ip();
            $params['type'] = $orderInfo['type'];
            $params['notify_url'] = 'http://yijiazhihui.40b876.cn/order/getWxPayNotify';
            $res = Payment::getWxAppPay($params);
            if (is_array($res)) {
                $paymemt['orderString'] = json_encode($res);
            } else {
                Db::rollback();
            }
        } elseif ($orderInfo['pay_type'] == 2) {
            //支付宝支付
            $params['title'] = $orderInfo['title'];
            $params['tid'] = $orderInfo['order_sn'];
            //                $params['fee'] = $order['pay_amount'];
            $params['fee'] = $orderInfo['total_amount'] * 100;
            $params['type'] = $orderInfo['type'];
            $params['notify_url'] = 'http://yijiazhihui.40b876.cn/order/getAliPayNotify';
            $res = Payment::getAliPay($params);
            if($res){
                $paymemt['orderString'] = $res;
            }else{
                Db::rollback();
            }
        } elseif ($orderInfo['pay_type'] == 3) {
            //金币支付
            if ($member['gold'] < $orderInfo['total_amount']) {
                Db::rollback();
                exception('金币不足');
            }
            $memberOk = Db::table('zh_members')->where('member_id', $orderInfo['member_id'])->update(['gold' => $memberInfo->gold - $orderInfo['total_amount']]);
            if (!$memberOk) {
                Db::rollback();
                exception('更新用户金币失败');
            }
            $orderOk = $MemberOrderVipModel->where('id', $orderid)->update(['status' => 2, 'update_time' => time(), 'pay_time' => time()]);
            if (!$orderOk) {
                Db::rollback();
                exception('更新订单状态失败');
            }
        } else {
            //钱包支付
            if ($memberInfo->account < $orderInfo['total_amount']) {
                Db::rollback();
                exception('余额不足');
            }
            $memberOk = Db::table('zh_members')->where('member_id', $orderInfo['member_id'])->update(['account' => $memberInfo->account - $orderInfo['total_amount']]);
            if (!$memberOk) {
                Db::rollback();
                exception('更新账户余额失败');
            }
            $orderOk = $MemberOrderVipModel->where('id', $orderid)->update(['status' => 2, 'update_time' => time()]);
            if (!$orderOk) {
                Db::rollback();
                exception('更新订单支付状态失败');
            }
        }
    }
    
    private function createNO($prefix)
    {
        $billno = date('YmdHis') . rand('100000', '999999');
        $order_sn = $prefix . $billno;
        return $order_sn;
    }

}